package com.coding.kan_yi_dao.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.coding.kan_yi_dao.common.Result;
import com.coding.kan_yi_dao.entity.*;
import com.coding.kan_yi_dao.mapper.GoodsOrderMapper;
import com.coding.kan_yi_dao.service.IGoodsOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 砍一刀，不买就砍你一刀
 * @since 2023-09-20
 */
@Service
public class GoodsOrderServiceImpl extends ServiceImpl<GoodsOrderMapper, GoodsOrder> implements IGoodsOrderService {
    @Resource
    GoodsOrderMapper goodsOrde;

    @Resource
    OrderItemServiceImpl orderItemService;

    @Resource
    GoodsInfoServiceImpl goodsInfoService;

    @Resource
    ShoppingCartItemServiceImpl shoppingCartItemService;
    //全查询
    @Override
    public Result go_select() {
        List<GoodsOrder> a = goodsOrde.goods_order_select();
        return Result.ok(a);
    }

    //修改
    @Override
    public Result go_update(GoodsOrder goodsOrder) {
        return Result.ok(baseMapper.updateById(goodsOrder));
    }

    //配货完成
    @Override
    public Result go_update_shop(List<Integer> ids) {
        List<GoodsOrder> goodsOrders = new ArrayList<>();
        for (int i = 0; i < ids.size(); i++) {
            GoodsOrder goodsOrder = new GoodsOrder();
            goodsOrder.setGoodsOrderId(ids.get(i));
            goodsOrder.setOrderStatus(2);
            goodsOrders.add(goodsOrder);
        }
        return Result.ok(updateBatchById(goodsOrders));
    }

    //出库
    @Override
    public Result go_update_ck(List<Integer> ids) {
        List<GoodsOrder> goodsOrders = new ArrayList<>();
        for (int i = 0; i < ids.size(); i++) {
            GoodsOrder goodsOrder = new GoodsOrder();
            goodsOrder.setGoodsOrderId(ids.get(i));
            goodsOrder.setOrderStatus(3);
            goodsOrders.add(goodsOrder);
        }
        return Result.ok(updateBatchById(goodsOrders));
    }

    @Override
    public Result go_deleteall(List<Integer> ids) {
        List<GoodsOrder> goodsOrders = new ArrayList<>();
        for (int i = 0; i < ids.size(); i++) {
            GoodsOrder goodsOrder = new GoodsOrder();
            goodsOrder.setGoodsOrderId(ids.get(i));
            goodsOrder.setIsDeleted(0);
            goodsOrders.add(goodsOrder);
        }
        return Result.ok(updateBatchById(goodsOrders));
    }

    @Override
    public Result go_insert(GoodsOrder goodsOrder,List<Integer> ids,List<Integer> counts) {
        Integer a  =RandomUtil.randomInt(1000000000);
        System.out.println(a);
        goodsOrder.setGoodsOrderId(a);
        baseMapper.insert(goodsOrder);
        for (int i = 0; i <ids.size() ; i++) {
            OrderItem orderItem = new OrderItem();
            orderItem.setGoodsOrderName(goodsInfoService.go_selects(ids.get(i)).getGoodsName());
            orderItem.setGoodsCoverImg(goodsInfoService.go_selects(ids.get(i)).getGoodsCoverImg());
            orderItem.setOrderId(a);
            orderItem.setGoodsId(ids.get(i));
            orderItem.setGoodsCount(counts.get(i));
            orderItemService.go_insert(orderItem);
            shoppingCartItemService.go_delete(ids);

        }

        return Result.ok("下单完成");
    }

    @Override
    public Result go_order_item(Integer b) {
        return Result.ok(goodsOrde.goods_order_item(b));
    }
}
